let util = require("../../../utils/util");
const app = getApp();
let footList;
let date;
Page({
    data: {
        date: util.formatTime(new Date()).split(" ")[0],
        calendarConfig: {
            defaultDay: false,
            highlightToday: true,
            inverse: true,
            onlyShowCurrentMonth: true,
        },
        show: false,

    },

    onLoad() {
        footList = ks.getStorageSync("footList");
        date = [];
        if (footList) {
            footList.forEach(item => {
                if (date.some(data => data.year == item.year && data.month == item.month && data.day == item.day))
                    return;
                else
                    date.push({ year: item.year, month: item.month, day: item.day, date: [] })
            });
            date.forEach(memory => {
                memory.date = footList.filter(item => item.year == memory.year && item.month == memory.month && item.day == memory.day)
            });
            this.setData({
                browList: date,
            });
        }
    },
    afterCalendarRender() {
        let days = [];
        if (footList) {
            this.setData({
                footList,
            });
            footList.forEach(item => {
                if (days.some(time => time.year == item.year && time.month == item.month && time.day == item.day))
                    return
                else days.push({ year: item.year, month: item.month, day: item.day })
            });
            this.calendar.setTodoLabels({
                pos: "top",
                dotColor: '#ff6600',
                circle: false,
                showLabelAlways: true,
                days,
            });
        }
    },

    clickDate(e) {
        let selectDay = this.calendar.getSelectedDay()[0];
        this.setData({
            browList: date.filter(item => selectDay.year == item.year && selectDay.month == item.month && selectDay.day == item.day),
            show: false,
            date: [selectDay.year, selectDay.month, selectDay.day].join("-"),
        });

    },

    delete(e) {
        let id = e.currentTarget.dataset.id;
        ks.showModal({
            title: '温馨提示',
            content: '是否确认删除此条浏览记录',
            showCancel: true,
            cancelText: '我再想想',
            cancelColor: '#000000',
            confirmText: '确定删除',
            confirmColor: '#3CC51F',
            success: (result) => {
                if (result.confirm) {
                    let footList = ks.getStorageSync("footList");
                    let delIndex = footList.findIndex(item => item.id == id);
                    if (delIndex >= 0) {
                        footList.splice(delIndex, 1);
                        ks.setStorageSync("footList", footList);
                        this.onLoad();
                    }
                }
            },
            fail: () => {},
            complete: () => {}
        });
    },


    toggleCalendar() {
        this.setData({
            show: !this.data.show,
        });
    }
});